﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaPortal.GUI.Library;
using System.Reflection;
using Action = MediaPortal.GUI.Library.Action;
using System.Threading;

namespace MediaPortal.Plugins.Sense
{
    public class MPSense : IPlugin, ISetupForm 
    {

        protected int pluginWindowId = 20101217;
        protected string pluginName;
        protected string pluginDescription;
        protected string pluginAuthor;
        protected SensorHandler sensor;
        protected bool isScrolling;
        protected string propertyPrefix = "#Sense.";

        public MPSense()
        {
            Assembly assy = Assembly.GetExecutingAssembly();
            foreach (Attribute attr in Attribute.GetCustomAttributes(assy))
            {
                if (attr.GetType() == typeof(AssemblyTitleAttribute))
                {
                    this.pluginName = ((AssemblyTitleAttribute)attr).Title;
                }
                else if (attr.GetType() == typeof(AssemblyDescriptionAttribute))
                {
                    this.pluginDescription = ((AssemblyDescriptionAttribute)attr).Description;
                }
                else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                {
                    this.pluginAuthor = ((AssemblyCompanyAttribute)attr).Company;
                }
            }
            
            sensor = new SensorHandler();
        }

        void IPlugin.Start()
        {
            Logger.Debug("Starting plugin.");
            sensor.Action += new Action<SensorEvent, bool>(OnSensorEvent);
            sensor.Start();
        }

        void OnSensorEvent(SensorEvent sensorAction, bool repeat)
        {
            switch (sensorAction)
            {
                case SensorEvent.SessionStart:
                    Util.Utils.PlaySound("Sense/SessionStart.wav", false, true);
                    SetGuiProperty("Session", "true");
                    return;
                case SensorEvent.SessionEnd:
                    Util.Utils.PlaySound("Sense/SessionEnd.wav", false, true);
                    SetGuiProperty("Session", "false");
                    return;
                case SensorEvent.ScrollStart:
                    isScrolling = true;
                    Util.Utils.PlaySound("Sense/ScrollStart.wav", false, true);
                    SetGuiProperty("Scrolling", "true");
                    return;
                case SensorEvent.ScrollEnd:
                    isScrolling = false;
                    Util.Utils.PlaySound("Sense/ScrollEnd.wav", false, true);
                    SetGuiProperty("Scrolling", "false");
                    return;
                case SensorEvent.ScrollConnected:
                    Util.Utils.PlaySound("Sense/ScrollConnected.wav", false, true);
                    return;
                case SensorEvent.ScrollReleased:
                    Util.Utils.PlaySound("Sense/ScrollReleased.wav", false, true);
                    return;
                case SensorEvent.Select:
                    if (GUIWindowManager.IsRouted)
                    {
                        Util.Utils.PlaySound("Sense/ClickSmall.wav", false, true);
                    }
                    else
                    {
                        Util.Utils.PlaySound("Sense/Click.wav", false, true);
                    }
                    break;
                case SensorEvent.Back:
                    Util.Utils.PlaySound("Sense/Back.wav", false, true);
                    break;
                case SensorEvent.Up:
                case SensorEvent.Down:
                case SensorEvent.Left:
                case SensorEvent.Right:
                    if (!isScrolling)
                    {
                        Util.Utils.PlaySound("Sense/Cursor.wav", false, true);
                    }
                    break;
            }

            Action action = new Action();
            action.fAmount1 = 0;
            action.fAmount2 = 0;

            // map the sensor action to a mediaportal action
            switch (sensorAction)
            {
                case SensorEvent.Up:
                    action.wID = Action.ActionType.ACTION_MOVE_UP;
                    break;
                case SensorEvent.Down:
                    action.wID = Action.ActionType.ACTION_MOVE_DOWN;
                    break;
                case SensorEvent.Left:
                    action.wID = Action.ActionType.ACTION_MOVE_LEFT;
                    break;
                case SensorEvent.Right:
                    action.wID = Action.ActionType.ACTION_MOVE_RIGHT;
                    break;
                case SensorEvent.Select:
                    action.wID = Action.ActionType.ACTION_SELECT_ITEM;
                    break;
                case SensorEvent.Back:
                    action.wID = Action.ActionType.ACTION_PREVIOUS_MENU;
                    break;
            }

            // send the mediaportal action
            GUIGraphicsContext.OnAction(action);
        }

        void IPlugin.Stop()
        {
            Logger.Debug("Stopping plugin.");
            sensor.Stop();
        }

        string ISetupForm.Author()
        {
            return this.pluginAuthor;
        }

        bool ISetupForm.CanEnable()
        {
            return true;
        }

        bool ISetupForm.DefaultEnabled()
        {
            return true;
        }

        string ISetupForm.Description()
        {
            return this.pluginDescription;
        }

        bool ISetupForm.GetHome(out string strButtonText, out string strButtonImage, out string strButtonImageFocus, out string strPictureImage)
        {
            strButtonText = string.Empty;
            strButtonImage = string.Empty;
            strButtonImageFocus = string.Empty;
            strPictureImage = string.Empty;
            return false;
        }

        int ISetupForm.GetWindowId()
        {
            return this.pluginWindowId;
        }

        bool ISetupForm.HasSetup()
        {
            return false;
        }

        string ISetupForm.PluginName()
        {
            return this.pluginName;
        }

        void ISetupForm.ShowPlugin()
        {
            return;
        }

        void SetGuiProperty(string property, string value) 
        {
            GUIPropertyManager.SetProperty(propertyPrefix + property, value);
        }
    }
}
